package com.hzw.saas.common.verify.service.impl;

import java.util.List;
import java.util.Objects;

import com.hzw.saas.common.config.util.RedisUtil;
import com.hzw.saas.common.util.LangUtil;
import com.hzw.saas.common.verify.enums.SmsType;
import com.hzw.saas.common.verify.service.IEmailService;
import com.hzw.saas.common.verify.service.ISmsService;
import com.hzw.saas.common.verify.service.IVerifyService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 验证服务默认实现
 *
 * @author zzl
 * @since 01/12/2021
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class VerifyServiceImpl implements IVerifyService {

    private static final int TIME_OUT = 60 * 15; // 超时时间（秒）

    private final IEmailService emailService;

    private final ISmsService smsService;

    @Override
    public boolean sendSmsVerifyCode(String phone, String code, SmsType smsType) {
        code = smsService.sendVerifyCode(phone, code, smsType);
        boolean result = StrUtil.isNotBlank(code);
        log.debug("send phone verify code: {phone: {}, code: {}}", phone, code);
        if (result) {
            RedisUtil.set(phone, code, TIME_OUT);
        }
        return result;
    }

    @Override
    public boolean sendSmsVerifyCode(String phone, SmsType smsType) {
        String code = LangUtil.getRandom("", 6);
        return sendSmsVerifyCode(phone, code, smsType);
    }

    @Override
    public boolean sendEmailVerifyCode(String address, String code) {
        code = emailService.sendVerifyCode(address, "Saas用户验证", code);
        log.debug("send phone verify code: {email: {}, code: {}}", address, code);
        if (StrUtil.isNotBlank(code)) {
            RedisUtil.set(address, code, TIME_OUT);
            return true;
        }
        return false;
    }

    @Override
    public boolean sendEmailVerifyCode(String email) {
        String code = LangUtil.getRandom("", 6);
        return sendEmailVerifyCode(email, code);
    }

    @Override
    public boolean verify(String key, String code) {
        return this.verify(key, code, true);
    }

    @Override
    public boolean verify(String key, String code, boolean deleteOnPass) {
        Object value = RedisUtil.get(key);
        log.debug("verify code: {key: {}, code: {}, realCode: {}}", key, code, value);
        if (value == null)
            return false;
        boolean verify = Objects.equals(String.valueOf(value), code);
        if (verify && deleteOnPass)
            this.delete(key);
        return verify;
    }

    @Override
    public void delete(String... keys) {
        List<String> list = CollectionUtil.list(false, keys);
        list.removeIf(StrUtil::isBlank);
        RedisUtil.del(list.toArray(new String[0]));
    }

    @Override
    public String get(String key) {
        Object o = RedisUtil.get(key);
        if (o != null) {
            return String.valueOf(o);
        }
        return null;
    }


}
